package ticketmanager;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

/**
 *
 * @author Viktor
 */
public class Main {
    /**
     * @param args the command line arguments
     */
    private static int seconds_left = 0;
    private static boolean paused = false;



    public static void updateForce()
    {
        seconds_left = 0;
    }

    /**!!NEED TO UPDATE JAVADOC!!
     *
     * main method for TT tracking launch
     */
    private static void process_TT_track()
    {

        List<Ticket> newTicketList = new ArrayList<Ticket>();
        List<Ticket> currentTicketList = new ArrayList<Ticket>();
        MainWindow mainWindow = new MainWindow();
        TicketManager ticketControl = new TicketManager();
        DatabaseConnector dbConnector = new DatabaseConnector();

        Integer newTicketCount = 0;
        Connection conn = null;

        mainWindow.setVisible(true);

        mainWindow.setConnectionStatusLabel("Loading driver..");
        try {
            Class.forName ("oracle.jdbc.driver.OracleDriver");
        } catch (ClassNotFoundException e) {
            System.out.println("Database connection driver not found!");
            System.exit(0);
        }

        mainWindow.setTrayIconImage(MainWindow.CONNECTION_LOST);
        mainWindow.setConnectionStatusLabel("Connecting..");



        // connecting
        conn = dbConnector.establishConnection();
        while(conn == null) {
            try {
                Thread.sleep(5000);
            } catch (InterruptedException e) {
                System.out.println("Something wrong happens with conn trying!");
            }
            conn = dbConnector.establishConnection();
        }




        mainWindow.setConnected(true);
        mainWindow.setTrayIconImage(MainWindow.IDLE_STATE);
        mainWindow.setConnectionStatusLabel("Connected");



        // main tt tracking cycle
        while(true) {
            newTicketList = ticketControl.getTickets(conn);
            if(newTicketList != null) {
                mainWindow.setLockTable(true);
                currentTicketList = mainWindow.retrieveTicketTable();
                newTicketList = ticketControl.mergeTickets(currentTicketList, newTicketList);
                newTicketCount = ticketControl.countFreshTickets(newTicketList);
                mainWindow.updateTicketTable(newTicketList);
                mainWindow.setLockTable(false);

                if(newTicketCount > 0) {
                    if(!mainWindow.isFocused()) {
                        mainWindow.setTrayIconImage(MainWindow.NEW_TICKETS);
                    }
                    mainWindow.displayTicketsBaloon(newTicketCount);
                }

                seconds_left = mainWindow.getRefreshRate();
            } else {
                mainWindow.setConnected(false);
                mainWindow.setConnectionStatusLabel("Disconnected");
                mainWindow.setTrayIconImage(MainWindow.CONNECTION_LOST);
                mainWindow.displayInfoBaloon(MainWindow.CONNECTION_LOST);
                dbConnector.closeConnection(conn);
                conn = dbConnector.establishConnection();
                while(conn == null) {
                    try {
                        Thread.sleep(10000);
                    } catch (InterruptedException e) {
                        System.out.println("Something wrong happens with conn trying!");
                    }
                    conn = dbConnector.establishConnection();
                }
                mainWindow.setConnected(true);
                mainWindow.setConnectionStatusLabel("Connected");
                mainWindow.setTrayIconImage(MainWindow.IDLE_STATE);
                mainWindow.displayInfoBaloon(MainWindow.IDLE_STATE);
                newTicketList = new ArrayList<Ticket>();
                seconds_left = 0;
            }

            while (seconds_left > 0) {
                try {
                    // sleep forever while status is paused
                    while (paused){
                        Thread.sleep(1000);
                    }
                    // displaying remaining time until next update
                    Thread.sleep(1000);
                    mainWindow.showTimeRemaining(seconds_left);
                    seconds_left--;

                } catch (InterruptedException e) {
                    System.out.println("Something wrong happens with delay!");
                }
            }
        }
    }


    public static void PauseUnpause()
    {
    paused = !paused;

    }

    public static void setPause(boolean aPause)
    {
    paused = aPause;

    }
    public static boolean getPaused()
    {
    return paused;
    }


    public static void main(String[] args) {
        // TODO code application logic here
        if(ChangeLAF.changeLAF(ChangeLAF.aluminiumLAF) == 0)
            ChangeLAF.changeLAF(null);
        process_TT_track();


    }

}

class ChangeLAF {
    public static final String aluminiumLAF = "com.jtattoo.plaf.aluminium.AluminiumLookAndFeel";

    public static int changeLAF(String laf) {
        try {
            if(laf == null)
                UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
            else
                UIManager.setLookAndFeel(laf);
        } catch(ClassNotFoundException e) {
            return 0;
        } catch (InstantiationException e) {
            return 0;
        } catch (IllegalAccessException e) {
            return 0;
        } catch (UnsupportedLookAndFeelException e) {
            return 0;
        }

        return 1;
    }
}
